function region() {
    // 获取三个省市县的select 标签
    var provinceSelect = document.querySelector('.region .provinceSelect')
    if (!provinceSelect) return
    var citySelect = document.querySelector('.region .citySelect')
    var countySelect = document.querySelector('.region .countySelect')
    // console.log('provinceSelect', provinceSelect);
    // console.log('citySelect', citySelect);
    // console.log('countySelect', countySelect);
    // 这个函数需要在页面加载完成就执行  把省份获取到并添加到省份的select标签里面
    function getProvince() {

        ajax({
            url: '/api/province',
            success: function (data) {

                data.forEach(item => {
                    // var option = document.createElement('option')
                    // option.innerHTML = `${item.name}`
                    // option.value = `${item.code}`
                    // provinceSelect.appendChild(option)

                    var option = `<option value="${item.code}">${item.name}</option>`;
                    provinceSelect.insertAdjacentHTML('beforeend', option)
                });
            }
        })
    }
    getProvince()

    function changeProvince() {
        provinceSelect.addEventListener('change', function (e) {
            // console.log(e.target.value);
            var code = e.target.value
            // 如果该省份没有市 就直接选择县
            if (code === "110000" || code === "120000" || code === "310000" || code === "500000") {
                citySelect.style.display = 'none'
                // console.log('----');
                var code = e.target.value
                ajax({
                    url: '/api/county',
                    data: {
                        code
                    },
                    success: function (data) {
                        countySelect.innerHTML = `<option value="-1">请选择区/县</option>`
                        data.forEach(item => {
                            var option = `<option value="${item.value}">${item.name}</option>`
                            countySelect.insertAdjacentHTML('beforeend', option)

                        });
                    }
                })
                return
            }

            ajax({
                url: '/api/city',
                data: {
                    code
                },
                success: function (data) {
                    // console.log('data', data);
                    // console.log("api/city");
                    citySelect.innerHTML = `<option value="-1">请选择市</option>`;
                    data.forEach(item => {
                        var option = `<option value="${item.code}">${item.name}</option>`;
                        citySelect.insertAdjacentHTML('beforeend', option)
                    });

                }
            })
        })
    }
    changeProvince()

    function changeCity() {
        citySelect.addEventListener('change', (e) => {

            var code = e.target.value
            ajax({
                url: '/api/county',
                data: {
                    code
                },
                success: function (data) {
                    countySelect.innerHTML = `<option value="-1">请选择区/县</option>`
                    data.forEach(item => {
                        var option = `<option value="${item.value}">${item.name}</option>`
                        countySelect.insertAdjacentHTML('beforeend', option)

                    });
                }
            })
        })
    }
    changeCity()
}
region()
